extends Node2D
@onready var test_7: Node2D = $"."

var item: Array[Node]   = []
var target_position     = Vector2()
var step                = 0
var click_position: int = 0


func _ready() -> void:
	for i in range(21):
		if i!=0:
			var right_1 = preload("res://layout/test8.tscn").instantiate()
			right_1.position=getVector(i)
			test_7.add_child(right_1)
			right_1.connect("my_signal", Callable(self, "_on_my_signal"))
			right_1.connect("my_ttttt", Callable(self, "_on_my_ttttt"))
			item.append(right_1)
			if i> 15:
				right_1.setImage("res://animation/test1.tres", "guard_3", "燕山雪"+str(i), i, true)
			elif i> 10:
				right_1.setImage("res://animation/test1.tres", "guard_3", "燕山雪"+str(i), i, true)
			elif i> 5:
				right_1.setImage("res://animation/test1.tres", "guard_7", "燕山雪"+str(i), i, true)
			else:
				right_1.setImage("res://animation/test1.tres", "guard_7", "燕山雪"+str(i), i, true)


func _process(delta: float) -> void:
	pass


func _physics_process(delta: float) -> void:
	if step == 1:
		item[17].position  = item[17].position.move_toward(target_position, 30)
		if item[17].position == target_position:
			step = 2
			showToa()
	elif step == 3:
		item[17].position  = item[17].position.move_toward(target_position, 30)


func getVector(index: float) -> Vector2:
	if index> 15:
		return Vector2(400 +580-((index-15) *60), ((index - 15)*100)+80)
	elif index> 10:
		return Vector2(400 + 480 - ((index - 10) * 60), ((index - 10)*100)+40)
	elif index> 5:
		return Vector2(400 + 100 - ((index - 5) * 60), ((index - 5)*100)+40)
	else:
		return Vector2(400 - (index * 60), (index * 100) )


func _on_my_signal(info: Vector2, size: Vector2, position: int):
	print('_on_my_signal')
	if click_position> 15:
		item[click_position-1].setImage("res://animation/test1.tres", "guard_3", "燕山雪"+str(click_position), click_position, true)
	elif click_position> 10:
		item[click_position-1].setImage("res://animation/test1.tres", "guard_3", "燕山雪"+str(click_position), click_position, true)
	elif click_position> 5:
		item[click_position-1].setImage("res://animation/test1.tres", "guard_7", "燕山雪"+str(click_position), click_position, true)
	else:
		item[click_position-1].setImage("res://animation/test1.tres", "guard_7", "燕山雪"+str(click_position), click_position, true)
	step = 1
	click_position=position
	target_position=Vector2(info.x+(size.x/2), info.y)


func _on_my_ttttt(position: int):
	print('_on_my_ttttt')
	if position> 15:
		item[position-1].setImage("res://animation/test1.tres", "guard_3", "燕山雪"+str(position), position, true)
	elif position> 10:
		item[position-1].setImage("res://animation/test1.tres", "guard_3", "燕山雪"+str(position), position, true)
	elif position> 5:
		item[position-1].setImage("res://animation/test1.tres", "guard_7", "燕山雪"+str(position), position, true)
	else:
		item[position-1].setImage("res://animation/test1.tres", "guard_7", "燕山雪"+str(position), position, true)
	if position ==17:
		step = 3
		target_position=getVector(18)
		item[17].setImage("res://animation/test1.tres", "guard_3", "燕山雪"+str(position), position, true)

func showToa():
	item[17].setImage("res://animation/test1.tres", "attack_9", "燕山雪17", 17, false)
	item[click_position-1].setImage("res://animation/test1.tres", "hit_7", "燕山雪"+str(click_position), click_position, false)
